#!/bin/bash

conf_kms_array=(
    'KMSAGENT_KEYSTORE KmcMainPath _agentka.ksf'
    'KMSAGENT_KEYSTORE KmcBackupPath _agentkb.ksf'
)

conf_ksf_item_array=("_agentka.ksf" "_agentkb.ksf")
function InitKmsagentConfig() {
    if [ ! -d ${kmsagent_work_path} ]; then
        mkdir -p ${kmsagent_work_path}
        if [ $? -ne 0 ]; then
            echo "[WARNING] Unable to create work dir."
            return 1
        fi
    fi

    local user_home_path=`eval echo "~${username}"`
    chown -fh ${username}:${usergroup} ${user_home_path}/kmsagent_*.log >& /dev/null
    chown -fh ${username}:${usergroup} ${kmsagent_work_path}/kmsagent_*.log >& /dev/null

    if [ ! -d ${kmsagent_work_path}/data ]; then
        mkdir -p ${kmsagent_work_path}/data
        if [ $? -ne 0 ]; then
            echo "[WARNING] Unable to create work data dir."
            return 1
        fi
    fi

    if [ -L ${kmsagent_work_path}/data ]; then
        echo "[WARNING] ${kmsagent_work_path##*/}/data cannot be a soft link."
        return 1
    fi

    if [ -L ${kmsagent_work_path}/backup_data ]; then
        echo "[WARNING] ${kmsagent_work_path##*/}/backup_data cannot be a soft link."
        return 1
    fi

    chown -h ${username}:${usergroup} ${kmsagent_work_path}
    chown -h ${username}:${usergroup} ${kmsagent_work_path}/data

    su - ${username} -s /bin/bash -c "chmod 700 ${kmsagent_work_path}/data" >/dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo "[WARNING] Unable to configure work data dir."
    fi
    su - ${username} -s /bin/bash -c "chmod 750 ${kmsagent_work_path}" >/dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo  "[WARNING] Unable to configure work dir."
    fi

    if [[ -d ${kmsagent_work_path}/backup_data && -f ${kmsagent_work_path}/backup_data/${username}${conf_ksf_item_array[0]} ]]; then
        for ksf_item in "${conf_ksf_item_array[@]}"; do
            rm -f ${kmsagent_work_path}/data/${username}${ksf_item} >/dev/null 2>&1
            mv -n ${kmsagent_work_path}/backup_data/${username}${ksf_item} ${kmsagent_work_path}/data/ >/dev/null 2>&1
            chown -fh ${username}:${usergroup} ${kmsagent_work_path}/data/${username}${ksf_item}
            su - ${username} -s /bin/bash -c "chmod 600 -f ${kmsagent_work_path}/data/${username}${ksf_item}" >/dev/null 2>&1
            if [ $? -ne 0 ]; then
                echo "[WARNING] Unable to configure ${username}${ksf_item}."
            fi
        done
        [ -d "${kmsagent_work_path}/backup_data" ] && [ `ls "${kmsagent_work_path}/backup_data" | wc -l` -eq 0 ] && rm -rf ${kmsagent_work_path}/backup_data
    fi

    cp -nf ${kmsagent_conf_path} ${kmsagent_work_path}/ >/dev/null 2>&1
    chown -fh ${username}:${usergroup} ${kmsagent_work_path}/kmsagent.conf
    su - ${username} -s /bin/bash -c "chmod 600 -f ${kmsagent_work_path}/kmsagent.conf" >/dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo "[WARNING] Unable to configure kmsagent.conf."
    fi

    touch ${kmsagent_work_path}/kmsconf.ksf
    if [ -L ${kmsagent_work_path}/kmsconf.ksf ]; then
        echo "[WARNING] ${kmsagent_work_path##*/}/kmsconf.ksf cannot be a soft link."
        return 1
    fi

    chown -h ${username}:${usergroup} ${kmsagent_work_path}/kmsconf.ksf
    su - ${username} -s /bin/bash -c "chmod 600 ${kmsagent_work_path}/kmsconf.ksf" >/dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo "[WARNING] Unable to configure main ksf."
    fi

    if [ -f ${kmsagent_work_path}/backup_kmsconf.ksf ]; then
        if [ -L ${kmsagent_work_path}/backup_kmsconf.ksf ]; then
            echo "[WARNING] ${kmsagent_work_path##*/}/backup_kmsconf.ksf cannot be a soft link."
            return 1
        fi
        chown -h ${username}:${usergroup} ${kmsagent_work_path}/backup_kmsconf.ksf
        su - ${username} -s /bin/bash -c "chmod 600 ${kmsagent_work_path}/backup_kmsconf.ksf" >/dev/null 2>&1
        if [ $? -ne 0 ]; then
            echo "[WARNING] Unable to configure backup ksf."
        fi
    fi

    systemctl status haveged >/dev/null 2>&1
    local haveged_status=$?

    if [ "${haveged_status}" = "0" ];then
        for ksf_param in "${conf_kms_array[@]}"; do
            sub_param=(${ksf_param})
            su - ${username} -s /bin/bash -c "export LD_LIBRARY_PATH=${kmsagent_lib_path}; ${kmsagent_path} -c "${kmsagent_work_path}/kmsagent.conf" -k ${kmsagent_work_path}/kmsconf.ksf -s ${sub_param[0]} -n ${sub_param[1]} -v ${kmsagent_work_path}/data/${username}${sub_param[2]}" >/dev/null 2>&1
            if [ $? -ne 0 ]; then
                echo "[WARNING] Unable to configure ${sub_param[0]}/${sub_param[1]} in the kmsagent file."
                return 1
            fi
        done

        if [ -d ${kmsagent_work_path}/backup_data ]; then
            ${kmsagent_restore_path} ${kmsagent_path} ${kmsagent_work_path}/backup_data/kmsagent.conf ${kmsagent_work_path}/backup_data/kmsconf.ksf ${kmsagent_work_path}/kmsagent.conf ${kmsagent_work_path}/kmsconf.ksf ${username} ${usergroup} ${kmsagent_lib_path}
            if [ $? -ne 0 ]; then
                echo "[WARNING] Unable to restore kmsagent conf from backup conf file."
                return 1
            fi

            rm -f ${kmsagent_work_path}/backup_data/kmsagent.conf
            rm -f ${kmsagent_work_path}/backup_data/kmsconf.ksf
            rm -f ${kmsagent_work_path}/backup_data/backup_kmsconf.ksf
            [ -d "${kmsagent_work_path}/backup_data" ] && [ `ls "${kmsagent_work_path}/backup_data" | wc -l` -eq 0 ] && rm -rf ${kmsagent_work_path}/backup_data
        fi
    else
        echo "[WARNING] Haveged is not running. The kmsagent configuration is not restored."
    fi
}

function CheckInputParamInvalid() {
    for var in ${kmsagent_path} ${kmsagent_conf_path} ${kmsagent_restore_path}
    do
        if [[ ! -f ${var} || -L ${var} ]]; then
            echo "[WARNING] ${var##*/} is not a file."
            return 1
        fi
    done

    if [ -L ${kmsagent_work_path} ]; then
        echo "[WARNING] ${kmsagent_work_path##*/} cannot be a soft link."
    fi

    if [[ -z ${username} || -z ${usergroup} || -z ${kmsagent_lib_path} || -z ${install_result} ]]; then
        echo "[WARNING] [username;usergroup;kmsagent_lib_path;install_result] is invalid."
        return 1
    fi

    if [[ ${install_result} != "true" && ${install_result} != "false" ]]; then
        echo "[WARNING] [install_result] Only true or false is supported."
        return 1
    fi
}

function usage() {
    echo "Usage:"
    echo "     kmsagent_install.sh [kmsagent_path] [kmsagent_conf_path] [kmsagent_work_path] [username] [usergroup] [kmsagent_lib_path] [kmsagent_restore_path] [install_result]"
    exit -1
}

if [ $# -ne 8 ]; then
    echo "Incorrect number of parameters."
    usage
fi

kmsagent_path=$1
kmsagent_conf_path=$2
kmsagent_work_path=$3
username=$4
usergroup=$5
kmsagent_lib_path=$6
kmsagent_restore_path=$7
install_result=$8

CheckInputParamInvalid
if [ $? -ne 0 ]; then
    exit -1
fi

if [ "${install_result}" = "false" ]; then
    exit 0
fi

InitKmsagentConfig
if [ $? -ne 0 ]; then
    echo "[WARNING] Unable to configure kmsagent init file and can't use the model protection function."
fi
